const isMobile = function () {
  const userAgentInfo = navigator.userAgent
  const Agents = ['Android', 'iPhone',
    'SymbianOS', 'Windows Phone',
    'iPad', 'iPod']
  let flag = true
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false
      break
    }
  }
  return !flag
}

const sort = arr => {
  for (let i = 0; i < arr.length; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] < arr[i]) {
        const temp = arr[i]
        arr[i] = arr[j]
        arr[j] = temp
      }
    }
  }
}

/**
 * 读取URL中参数
 * @param qs
 * @returns {string}
 */
const input = (qs) => {
  const s = window.location.href
  let a = s.replace('?', '?&').split('&')
  var re = ''
  for (let i = 1; i < a.length; i++) {
    if (a[i].indexOf(qs + '=') === 0) {
      re = a[i].replace(qs + '=', '')
    }
  }
  return re
}

const validatePhone = (phone) => {
  return /^1[3456789]\d{9}$/.test(phone)
}

export default {
  isMobile,
  sort,
  validatePhone,
  input
}
